package com.team01.pokemon.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.team01.pokemon.entity.Pokeballs;
import com.team01.pokemon.result.Result;
import com.team01.pokemon.service.PokeballsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 精灵球表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-09-13
 */
@RestController
@RequestMapping("/pokeballs")
public class PokeballsController {

    @Autowired
    private PokeballsService pokeballsService;

    /**
     * 查询精灵球列表
     * 
     * @param query    可选的搜索关键词
     * @param category 可选的分类筛选
     * @return 精灵球列表
     */
    @GetMapping
    public Result list(String query, String category) {
        // 封装查询条件
        LambdaQueryWrapper<Pokeballs> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasLength(query), Pokeballs::getPokeballName, query);
        wrapper.eq(StringUtils.hasLength(category), Pokeballs::getCategory, category);

        // 执行查询
        List<Pokeballs> list = pokeballsService.list(wrapper);
        if (!CollectionUtils.isEmpty(list)) {
            return new Result(true, "查询精灵球列表成功", list);
        }
        return new Result(false, "查询精灵球列表失败", null);
    }

    /**
     * 按分类查询精灵球列表
     * 
     * @param category 精灵球分类
     * @return 精灵球列表
     */
    @GetMapping("/category/{category}")
    public Result listByCategory(@PathVariable String category) {
        // 封装查询条件
        LambdaQueryWrapper<Pokeballs> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Pokeballs::getCategory, category);

        // 执行查询
        List<Pokeballs> list = pokeballsService.list(wrapper);
        if (!CollectionUtils.isEmpty(list)) {
            return new Result(true, "查询分类精灵球列表成功", list);
        }
        return new Result(false, "查询分类精灵球列表失败", null);
    }
}
